package com.zzuli.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzuli.dao.RepairInfoDao;
import com.zzuli.entity.AreaInfoEntity;
import com.zzuli.entity.DeviceInfoEntity;
import com.zzuli.entity.RepairInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepairInfoService {
    @Autowired
    private RepairInfoDao repairInfoDao;

    public Map<String, Object> selectAll(int pageNum, int pageSize, RepairInfoEntity repairInfoEntity) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<RepairInfoEntity> list = repairInfoDao.selectAll(repairInfoEntity);
        //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<RepairInfoEntity> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("list", list);
        //总数据条目
        map.put("total", total);
        return map;
    }

    public boolean addRepair(Map<String, Object> map) {
        AreaInfoEntity areaInfoEntity = (AreaInfoEntity)map.get("areaInfoEntity");
        DeviceInfoEntity deviceInfoEntity = (DeviceInfoEntity) map.get("deviceInfoEntity");
        RepairInfoEntity repairInfoEntity = (RepairInfoEntity) map.get("repairInfoEntity");

        String remark = repairInfoEntity.getRemark();
        String spLocation = repairInfoEntity.getSpLocation();
        String deviceName = deviceInfoEntity.getDeviceName();

        return repairInfoDao.addRepair(deviceName);
    }

    public boolean updateRepair(RepairInfoEntity repairInfoEntity) {
        return repairInfoDao.updateByPrimaryKeySelective(repairInfoEntity);
    }

    public boolean deleteRepair(String[] allRepairId) {
        for (String repairId : allRepairId){
            repairInfoDao.deleteByPrimaryKey(Integer.parseInt(repairId));
        }
        return true;
    }

    public boolean updateRepairStatus(RepairInfoEntity repairInfoEntity) {
        return repairInfoDao.updateRepairStatus(repairInfoEntity);
    }

    public List<Integer> selectStatusCount() {
        return repairInfoDao.selectStatusCount();
    }

    public boolean accept(RepairInfoEntity repairInfoEntity) {
        return repairInfoDao.accept(repairInfoEntity);
    }
}
